﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OauthProvider;

namespace Chat_WebServer
{
    public class TokenUtility
    {
        //
        // GET: /TokenUtility/
        public static TokenBase GetToken(string token, String typeToken)
        {
            using (var db = new AccountEntities())
            {
                TokenBase Token = null;
                if (typeToken == TokenType.Request)
                {
                    RequestToken tk = db.RequestTokens.ToList().FirstOrDefault(r => r.Token.Trim() == token);
                    if (tk != null)
                        Token = ConvertToMiniRequestToken(tk);
                }
                if (typeToken == TokenType.Access)
                {
                    AccessToken tk = db.AccessTokens.ToList().FirstOrDefault(r => r.Token.Trim() == token);
                    if (tk != null)
                        Token = ConvertToMiniAccessToken(tk);
                }
                return Token;
            }
        }

        public static MiniRequestToken GetRequestTokenWithAccessToken(string accessToken)
        {
            using (var db = new AccountEntities())
            {
                RequestToken tk = db.RequestTokens.ToList().FirstOrDefault(r => r.AccessToken.Trim() == accessToken);
                MiniRequestToken Token = (MiniRequestToken)ConvertToMiniRequestToken(tk);
                return Token;
            }
        }

        private static TokenBase ConvertToMiniRequestToken(RequestToken tk)
        {
            MiniRequestToken Token = new MiniRequestToken();
            Token.ConsumerKey = tk.ConsumerKey;
            Token.Realm = tk.Realm;
            Token.Token = tk.Token;
            Token.TokenSecret = tk.TokenSecret;
            Token.AccessDenied = bool.Parse(tk.AccessDenied);
            if (tk.AccessToken1 != null)
                Token.AccessToken = (MiniAccessToken)ConvertToMiniAccessToken(tk.AccessToken1);
            Token.CallbackUrl = tk.CallbackUrl;
            Token.UsedUp = bool.Parse(tk.UsedUp);
            Token.Verifier = tk.Verifier;
            return Token;
        }

        private static TokenBase ConvertToMiniAccessToken(AccessToken tk)
        {
            MiniAccessToken Token = new MiniAccessToken();
            Token.ConsumerKey = tk.ConsumerKey;
            Token.Realm = tk.Realm;
            Token.Token = tk.Token;
            Token.TokenSecret = tk.TokenSecret;
            Token.UserName = tk.UserName;
            Token.ExpireyDate = tk.ExpireyDate;
            return Token;
        }

        public static void RemoveToken(string accessToken)
        {
            try
            {
                using (var db = new AccountEntities())
                {
                    RequestToken tk = db.RequestTokens.ToList().FirstOrDefault(r => r.AccessToken.Trim() == accessToken);
                    if (tk != null)
                    {
                        db.RequestTokens.DeleteObject(tk);
                        db.SaveChanges();
                        AccessToken actk = db.AccessTokens.ToList().FirstOrDefault(r => r.Token.Trim() == accessToken);
                        db.AccessTokens.DeleteObject(actk);
                        db.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }
    }
}
